from django import forms
from . import models
from django.core.validators import RegexValidator


class RegisterForm(forms.ModelForm):
    mobile_phone = forms.CharField(
        label="手机",
        widget=forms.TextInput(attrs={"class": "form-control"}),
        validators=[RegexValidator(r"^1(3|4|5|6|7|8|9)\d{9}$", "手机号格式错误"), ]
    )
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(attrs={"class": "form-control"})
    )
    password = forms.CharField(
        label="密码",
        widget=forms.PasswordInput(attrs={"class": "form-control"})
    )
    code = forms.CharField(
        label="验证码"
    )

    class Meta:
        model = models.UserInfo
        fields = ["username", "password", "confirm_password", "mobile_phone", "code", "email"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = "请输入{}".format(field.label)
